package com.shanghaionstar.vehicle_ownerlife;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.vehicle.entity.DiagnosticElement;
import com.onstar.cn.vehicle.response.DiagnosticResponse;
import com.shanghaionstar.carmanage.CheckMaintainActivity;
import com.shanghaionstar.carmanage.iview.ICarStatusView;
import com.shanghaionstar.carmanage.presenter.CarStatusPresenter;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.home.model.DiagnosticResponses;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;

import java.util.List;

/**
 * Created by Yonglang Hong on 2017/1/12.
 */
public class VehicleCheckMaintainHelper {

    /**
     * 异常数据标识
     */
    private static final String IS_DIRTY_DATA = "isDirtyData";

    Bundle checkMaintainBundle;

    boolean isEnable = false;

    CarStatusPresenter carStatusPresenter;

    BaseActivity baseActivity;

    public VehicleCheckMaintainHelper(BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
        String vin = ApplicationUtil.getInstance().getVin();

        getCarStatusPresenter().getCarStatusFromDb(vin);
    }

    private CarStatusPresenter getCarStatusPresenter() {
        checkMaintainBundle = new Bundle();
        if (carStatusPresenter == null) {
            carStatusPresenter = new CarStatusPresenter(baseActivity.getScheduler(), baseActivity);
            carStatusPresenter.setStatusView(new ICarStatusView() {
                @Override
                public void showCarStatus(DiagnosticResponses carStatus) {
                    setUpBundle(carStatus);
                }

                @Override
                public void showNoData() {
                    checkMaintainBundle.clear();
                }

                @Override
                public void showOnErr(Throwable throwable) {
                    checkMaintainBundle.clear();
                }

                @Override
                public void showCompleteTime(String time) {

                }

                @Override
                public void showLoding() {

                }
            });
        }
        return carStatusPresenter;
    }

    private void setUpBundle(DiagnosticResponses carStatus) {
        List<DiagnosticResponse> diagnosticResponses = carStatus.getDiagnosticResponse();
        if (diagnosticResponses != null && diagnosticResponses.size() > 0) {
            checkMaintainBundle.clear();                //清空维修保养建议所传参数
            showPsiStatus(diagnosticResponses);         //显示胎压信息
            if (ApplicationUtil.getInstance().isPhev()) {
                showHevCarStatus(diagnosticResponses);  //显示phev车况数据
            } else {
                showFuelCarStatus(diagnosticResponses); //显示非phev车况数据
            }
            enableCheckBtn();
        }
    }

    /**
     * 若不是脏数据则设置查看维修保养建议按钮可点击
     */
    private void enableCheckBtn() {
//        if (!checkMaintainBundle.isEmpty() && !checkMaintainBundle.getBoolean(IS_DIRTY_DATA, false)) {
//            isEnable = true;
//            if (checkEnableListener != null)
//                checkEnableListener.onEnble();
//        }
        if (checkMaintainBundle.getBoolean(IS_DIRTY_DATA, false)){
            checkMaintainBundle.clear();
        }
        if (checkEnableListener != null)
            checkEnableListener.onEnble();
    }

    CheckEnableListener checkEnableListener;

    public void setCheckEnableListener(CheckEnableListener checkEnableListener) {
        this.checkEnableListener = checkEnableListener;
    }

    public interface CheckEnableListener {
        void onEnble();
    }


    /**
     * 显示胎压信息
     */
    private void showPsiStatus(List<DiagnosticResponse> diagnosticResponses) {
        for (DiagnosticResponse psiBean : diagnosticResponses) {
            if (TextUtils.equals("TIRE PRESSURE", psiBean.getName())) { //胎压
                List<DiagnosticElement> dEleBeanList = psiBean.getDiagnosticElement();
                if (dEleBeanList != null && dEleBeanList.size() > 0) {
                    for (DiagnosticElement dEleBean : dEleBeanList) {
                        //返回胎压数据有异常，则给checkMaintainBundle的数据标识为脏数据
                        if (!CommonUtil.isNumerice(dEleBean.getValue())) {
                            checkMaintainBundle.putBoolean(IS_DIRTY_DATA, true);
                            break;
                        }
                        switch (dEleBean.getName().toUpperCase()) {
                            case "TIRE PRESSURE LF":               //左前轮
                                checkMaintainBundle.putString(Constants.TIRE_PRESSURE_LF, dEleBean.getMessage());
                                break;
                            case "TIRE PRESSURE LR":               //左后轮
                                checkMaintainBundle.putString(Constants.TIRE_PRESSURE_LR, dEleBean.getMessage());
                                break;
                            case "TIRE PRESSURE RF":               //右前轮
                                checkMaintainBundle.putString(Constants.TIRE_PRESSURE_RF, dEleBean.getMessage());
                                break;
                            case "TIRE PRESSURE RR":               //右后轮
                                checkMaintainBundle.putString(Constants.TIRE_PRESSURE_RR, dEleBean.getMessage());
                                break;
                        }
                    }
                }
                break;
            }
        }
    }

    /**
     * 显示非PHEV车辆车况信息
     */
    private void showFuelCarStatus(List<DiagnosticResponse> diagnosticResponses) {
        for (DiagnosticResponse responseBean : diagnosticResponses) {
            switch (responseBean.getName()) {
                case "OIL LIFE":            //机油寿命
                    setOilLife(responseBean);
                    break;
                case "ODOMETER":            //总里程数
                    setOdoMeter(responseBean);
                    break;
                case "FUEL TANK INFO":      //燃油余量
                    setFuelTankInfo(responseBean);
                    break;
            }
        }
    }

    /**
     * 显示混合动力类型车辆综合信息
     */
    private void showHevCarStatus(List<DiagnosticResponse> diagnosticResponses) {
        for (DiagnosticResponse responseBean : diagnosticResponses) {
            switch (responseBean.getName().toUpperCase()) {
                case "EV BATTERY LEVEL":    //电池电量
                    setBattery(responseBean);
                    break;
                case "OIL LIFE":            //机油寿命
                    setOilLife(responseBean);
                    break;
                case "FUEL TANK INFO":      //燃油余量
                    setFuelTankInfo(responseBean);
                    break;
                case "ODOMETER":            //总里程数
                    setOdoMeter(responseBean);
                    break;
            }
        }
    }


    /**
     * 电池电量
     */
    private void setBattery(DiagnosticResponse responseBean) {
        List<DiagnosticElement> batteryList = responseBean.getDiagnosticElement();
        if (batteryList != null && !batteryList.isEmpty()) {
            DiagnosticElement batteryBean = batteryList.get(0);
            checkMaintainBundle.putString(Constants.EV_BATTERY_LEVEL, batteryBean.getValue());
        }
    }

    /**
     * 机油寿命
     */
    private void setOilLife(DiagnosticResponse responseBean) {
        List<DiagnosticElement> oilList = responseBean.getDiagnosticElement();
        if (oilList != null && !oilList.isEmpty()) {
            DiagnosticElement oilBean = oilList.get(0);
            int oilLife = (int) Math.round(Double.valueOf(oilBean.getValue()));
            checkMaintainBundle.putInt(Constants.OIL_LIFE, oilLife);
        }
    }

    /**
     * 燃油余量
     */
    private void setFuelTankInfo(DiagnosticResponse responseBean) {
        List<DiagnosticElement> fuelList = responseBean.getDiagnosticElement();
        if (fuelList != null && !fuelList.isEmpty()) {
            for (DiagnosticElement elementBean : fuelList) {
                if ("FUEL LEVEL".equals(elementBean.getName().toUpperCase())) {
                    if (!TextUtils.isEmpty(elementBean.getValue())) {
                        if (CommonUtil.isNumerice(elementBean.getValue())) {
                            float fuel_value = Float.valueOf(elementBean.getValue());
                            fuel_value = Math.round(fuel_value);
                            fuel_value = fuel_value <= 0 ? 0 : fuel_value;
                            checkMaintainBundle.putInt(Constants.FUEL_TANK_INFO, (int) fuel_value);
                        }
                    }
                    break;
                }
            }
        }
    }

    /**
     * 总里程
     */
    private void setOdoMeter(DiagnosticResponse responseBean) {
        List<DiagnosticElement> odoList = responseBean.getDiagnosticElement();
        if (odoList != null && !odoList.isEmpty()) {
            DiagnosticElement odoBean = odoList.get(0);
            int totalMiles = Double.valueOf(odoBean.getValue()).intValue();
            checkMaintainBundle.putInt(Constants.ODOMETER, totalMiles);
        }
    }

    public boolean isEnable() {
        return true;//isEnable
    }

    public Bundle getCheckMaintainBundle() {
        return checkMaintainBundle;
    }

    /**
     * 跳转维修保养建议页面
     */
    public void gotoCheckMaintain() {
        if (isEnable()) {
            BusinessUtil.sendReportByClickType(baseActivity, FunctionNameFindId.CarConditions_MaintenanceSgst.getStringValue());
            Intent intent = new Intent(baseActivity, CheckMaintainActivity.class);
            intent.putExtras(checkMaintainBundle);
            baseActivity.startActivity(intent);
        }
    }
}
